<?php
require_once( './includes/WebStart.php' );


if( is_numeric($_GET[id_file])) {
  
  $url = $_GET[url];
  
  $id_file = mysql_real_escape_string($_GET[id_file]);
  $datenow = date("Y-m-d H:i:s", time() );
    
   if ($url == 'ed2k') {
      	$ed2k = 1;
      }
      
   elseif ($url == 'magnet') {
      	$magnet = 1;
      }
   else {
     die();
   }
  
  
  
    $dbr = wfGetDB( DB_SLAVE );
    $sql = "SELECT ed2k, magnet FROM ss_file_downloads WHERE id_file = '$id_file'";
    
    $res = $dbr->query( $sql );
    
    if ( $row = $dbr->fetchObject( $res ) ) { //if exists , update the counter +1
      
      
     $ed2k = $ed2k+$row->ed2k;
     $magnet = $magnet+$row->magnet;
      
      
      $dbr->freeResult( $res );
      
      $dbw =& wfGetDB( DB_MASTER );
      $dbw->begin();  
       
      $sql = "UPDATE ss_file_downloads SET ed2k = '$ed2k' , magnet = '$magnet'
             WHERE id_file = '$id_file'";
      
      
      $dbw->query( $sql );
		  $dbw->commit();
      
      
    
    } else { //if not exists the query search, create new row
      
      $dbw =& wfGetDB( DB_MASTER );
      $dbw->begin();     
      $sql = "INSERT INTO ss_file_downloads (id_file, date, ed2k, magnet)
            VALUES ('$id_file' , '$datenow','$ed2k' ,'$magnet')";
       
      $dbw->query( $sql );
      $dbw->commit();
    } 
  
    //echo "ok";

} else {
  
  die(-1);
  
}